﻿using QG.JobBack.BLL.IService;
using QG.JobBack.Core;
using QG.JobBack.DAL;
using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JobBack.Controllers
{
    public class FunctionController : Controller
    {
        private IFunctionService _ifunctionService;
        private IMenuInfoService _imenuinfoService;
        public FunctionController(IFunctionService ifunctionService, IMenuInfoService imenuinfoService)
        {
            _ifunctionService = ifunctionService;
            _imenuinfoService = imenuinfoService;
        }
        /// <summary>
        /// 功能页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetFunctionList(int page,int limit,string functionName)
        {
            ResponseResult<Function> result = _ifunctionService.GetFunctionList(page, limit, functionName);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        
        public ActionResult Add()
        {
            //获取所有字菜单
            List<MenuInfo> menus = _imenuinfoService.GetChildMenu().data;
            ViewBag.menuList = menus.Where(a=>a.ParentId!=1).ToList();
            return View();
        }

        public ActionResult Update(int id)
        {
            //获取所有字菜单
            List<MenuInfo> menus = _imenuinfoService.GetChildMenu().data;
            ViewBag.menuList = menus;
            //获取功能对象
            ResponseResult<Function> result = _ifunctionService.GetFunction(id);

            return View(result.data);
        }
        /// <summary>
        /// 删除一个功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteFunctionById(int id)
        {
            ResponseResult<Function> result = _ifunctionService.DeleteFunctionById(id);
            return Json(result);
        }
        /// <summary>
        /// 添加一个功能
        /// </summary>
        /// <param name="function"></param>
        /// <returns></returns>
        public JsonResult AddFunction(Function function)
        {
            ResponseResult<Function> result = _ifunctionService.AddFunction(function);
            return Json(result);
        }

        /// <summary>
        /// 添加一个功能
        /// </summary>
        /// <param name="function"></param>
        /// <returns></returns>
        public JsonResult UpdateFunction(Function function)
        {
            ResponseResult<Function> result = _ifunctionService.UpdateFunction(function);
            return Json(result);
        }
    }
}